package com.travel.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.travel.bean.User;
import com.travel.service.LoginService;
import com.travel.service.UserService;
import com.travel.utils.PageHelper;

@Controller
@RequestMapping("user")
public class UserController {
	
	@Resource(name="userServiceImpl")
	private UserService userService;
	@Resource(name="loginServiceImpl")
	private LoginService loginService;
	
	/**
	 * 用户登录
	 * @param phone
	 * @param pwd
	 * @param session
	 * @return
	 */
	@RequestMapping("login.html")
	public String login(String path ,Integer id,String phone,String pwd,HttpSession session){
		User user=loginService.login(phone, pwd);
		if(user!=null){
			session.setAttribute("user", user);
			if(path!=null&&!"".equals(path)){
				if(id!=null&&id!=0){
					return "redirect:/comm/"+path+".html//"+id;
				}
				return "redirect:/comm/"+path+".html/1";
			}
			return "redirect:/comm/index.html";
		}else{
			return "jsp/login";
		}
	}
	/**
	 * 用户注册controller实现
	 * @param user
	 * @return
	 */
	@RequestMapping("register.html")
	public String register(User user){
		
		try {
			loginService.saveEntry(user);
			return "redirect:comm/index.html";
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "jsp/register";
	}
	/**
	 * 修改用户信息
	 */
	@RequestMapping("/modify.do")
	@ResponseBody
	public Map modifyUser(String realname,String phone, String email,HttpSession session){
		Map jsonMap =new HashMap();
		String str="";
		User user=new User();
		user.setId(((User)session.getAttribute("user")).getId());
		user.setRealName(realname);
		user.setPhone(phone);
		user.setEmail(email);
		try {
			userService.updateEntry(user);
			str="修改成功，请填写你的旅客信息！";
		} catch (Exception e) {
			// TODO Auto-generated catch block
			str="修改失败，请稍后再试！";
			e.printStackTrace();
		}
		return jsonMap;
	}
	/**
	 * 跳转到后台查看用户数据展示
	 * @return
	 */
	@RequestMapping("/toviewuser.html")
	public String toViewUser(){
		return "admin/viewuser";
	}
	/**
	 * 后台用户数据展示查询
	 * @param page
	 * @param limit
	 * @return
	 */
	//用户后台实现
	@RequestMapping("/viewuser.do")
	@ResponseBody
	public Map viewUser(Integer page,Integer limit){
		PageHelper pageh=userService.getUserByMap(null, page, limit);
		List list=new ArrayList();
		for(User user:(List<User>)pageh.getList()){
			Map usermap=new HashMap();
			usermap.put("id", user.getId());
			usermap.put("realname", user.getRealName());
			usermap.put("email", user.getEmail());
			usermap.put("phone", user.getPhone());
			usermap.put("ipadress", user.getIpAddress());
			usermap.put("regTime", user.getRegTime());
			usermap.put("updateTime", user.getUpdTime());
			usermap.put("identity", user.getIdentity().getIdentity());
			usermap.put("cardno", user.getCardNo());
			list.add(usermap);
		}
		
		Map map=new HashMap();
		map.put("code", 0);
        map.put("msg", "叮叮当当");
        map.put("count", pageh.getTotalPageCount());
        map.put("data", list);
		
		return map;
	}
}
